﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class AuditionSubEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request.QueryString["id"];
                if(!string.IsNullOrEmpty(id))
                {
                    ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                    AuditionSub auditionSub= ctx.AuditionSubs.Single(r => r.Id == int.Parse(id));

                    Interviewer.Value = auditionSub.Interviewer;
                    InterviewTime.Value = auditionSub.InterviewTime.ToString("yyyy-MM-dd HH:mm:ss");
                    Result.Value = auditionSub.Result;
                    Remarks.Value = auditionSub.Remarks;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            AuditionSub auditionSub;
            string id = Request.QueryString["id"];
            if (string.IsNullOrEmpty(id))
            {
                auditionSub = new AuditionSub();
                auditionSub.Pid = int.Parse(Request.QueryString["pId"]);
                ctx.AuditionSubs.InsertOnSubmit(auditionSub);
            }
            else
            {
                auditionSub = ctx.AuditionSubs.Single(r => r.Id == int.Parse(id));
            }
            auditionSub.Interviewer = Interviewer.Value;
            auditionSub.InterviewTime = DateTime.Parse(InterviewTime.Value);
            auditionSub.Result = Result.Value;
            auditionSub.Remarks = Remarks.Value;
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }
    }
}